package com.example.stars_project_android.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import androidx.fragment.app.ListFragment;

import com.example.stars_project_android.R;
import com.example.stars_project_android.adapter.ConcernStarListAdapter;
import com.example.stars_project_android.adapter.GuestBookAdapter;
import com.example.stars_project_android.entity.GuestBookEntity;
import com.example.stars_project_android.index.Postmessage;
import com.example.stars_project_android.util.Constant;
import com.sackcentury.shinebuttonlib.ShineButton;

import java.util.ArrayList;
import java.util.List;

/**
 * 留言板的fragment
 */
public class GuestbookFragment extends ListFragment {

    private ShineButton likeBtn;
    private ListView listview;
    private List<GuestBookEntity> myList = new ArrayList<>();
    private TextView tvRemain,tvLikeNumber;   //提示文本
    private ImageButton GuestBookToRoomDetail, share;//返回到首页
    private Button MessageDelete;//删除小提示按钮
    private Button PostGuestBook;//发布留言按钮
    private boolean shineBtn = false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setView();
        //设置数据源
        myList = setData();
        //初始化适配器
        GuestBookAdapter guestBookAdapter = new GuestBookAdapter(
                getActivity(),
                R.layout.guestbook_item,
                myList
        );

        //绑定适配器
//        listview.setAdapter(guestBookAdapter);
        this.setListAdapter(guestBookAdapter);
        View view = View.inflate(getContext(),R.layout.guestbook_item,null);
        likeBtn = view.findViewById(R.id.btn_heart);
        PostGuestBook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), Postmessage.class);
                startActivity(intent);
            }
        });
        guestBookAdapter.setOnItemDeleteClickListener(new ConcernStarListAdapter.onItemDeleteListener() {
            @Override
            public void onDeleteClick(int i) {
                guestBookAdapter.notifyDataSetChanged();
            }
        });
//        likeBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (shineBtn = false){
////                    String number = tvLikeNumber.getText().toString();
////                    int num = Integer.parseInt(number) + 1;
//                    tvLikeNumber.setText((Integer.valueOf(tvLikeNumber.getText().toString()) + 1) + "");
//                    shineBtn = true;
//                }else{
////                    String number = tvLikeNumber.getText().toString();
////                    int num = Integer.parseInt(number) - 1;
//                    tvLikeNumber.setText((Integer.valueOf(tvLikeNumber.getText().toString()) - 1) + "");
//                    shineBtn = false;
//                }
//            }
//        });
    }

    @Override
    public void onResume() {
        super.onResume();
        myList = setData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_guestbook, container, false);
        return rootView;
    }

    private List<GuestBookEntity> setData() {
        if (Constant.guestBookList.size() == 0) {
            List<GuestBookEntity> myList = new ArrayList<>();

            GuestBookEntity guestBook = new GuestBookEntity();
            guestBook.setUserImg(R.mipmap.user_img1);
            guestBook.setUserId("HDK 4326-131-4");
            guestBook.setUserMessage("今天的天气很不错哦，适合出游！");
            guestBook.setMessageTime("2022-12-29 13:19:43");
            guestBook.setLikeNum("34");
            myList.add(guestBook);

            GuestBookEntity guestBook5 = new GuestBookEntity();
            guestBook5.setUserImg(R.mipmap.user_img1);
            guestBook5.setUserId("HD 489-1622-1");
            guestBook5.setUserMessage("愿你在今日能与开心和快乐撞个满怀!");
            guestBook5.setMessageTime("2022-12-28 15:17:23");
            guestBook5.setLikeNum("16");
            myList.add(guestBook5);

            GuestBookEntity guestBook1 = new GuestBookEntity();
            guestBook1.setUserImg(R.mipmap.user_img3);
            guestBook1.setUserId("LSM J2043-5610");
            List<Bitmap> list = new ArrayList<>();
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.qing);
            list.add(bitmap);
            guestBook1.setImg(list);
            guestBook1.setUserMessage("卿卿日常真的太好看啦！");
            guestBook1.setMessageTime("2022-12-27 12:04:32");
            guestBook1.setLikeNum("308");
            myList.add(guestBook1);

            GuestBookEntity guestBook6 = new GuestBookEntity();
            guestBook6.setUserImg(R.mipmap.user_img1);
            guestBook6.setUserId("HD 489-1622-1");
            guestBook6.setUserMessage("卡塔尔世界杯！！！！");
            guestBook6.setMessageTime("2022-12-18 21:10:36");
            guestBook6.setLikeNum("359");
            myList.add(guestBook6);

            GuestBookEntity guestBook7 = new GuestBookEntity();
            guestBook7.setUserImg(R.mipmap.user_img1);
            guestBook7.setUserId("HD 489-1622-1");
            guestBook7.setUserMessage("又是熬夜的一天。。。");
            guestBook7.setMessageTime("2022-12-16 23:16:31");
            guestBook7.setLikeNum("27");
            myList.add(guestBook7);

            GuestBookEntity guestBook2 = new GuestBookEntity();
            guestBook2.setUserImg(R.mipmap.user_img2);
            guestBook2.setUserId("BDL 4726-134-1");
            guestBook2.setUserMessage("天冷了，注意保暖哦！");
            guestBook2.setMessageTime("2022-12-12 06:28:04");
            guestBook2.setLikeNum("13");
            myList.add(guestBook2);

            GuestBookEntity guestBook3 = new GuestBookEntity();
            guestBook3.setUserImg(R.mipmap.user_img3);
            guestBook3.setUserId("LSM J2043-5610");
            guestBook3.setUserMessage("有什么好看的剧嘛？");
            guestBook3.setMessageTime("2022-12-02 08:27:13");
            guestBook3.setLikeNum("13");
            myList.add(guestBook3);

            GuestBookEntity guestBook8 = new GuestBookEntity();
            guestBook8.setUserImg(R.mipmap.user_img1);
            guestBook8.setUserId("HD 489-1622-1");
            guestBook8.setUserMessage("想一个人撑着伞去江南走走！");
            guestBook8.setMessageTime("2022-11-24 07:45:02");
            guestBook8.setLikeNum("30");
            myList.add(guestBook8);

            GuestBookEntity guestBook4 = new GuestBookEntity();
            guestBook4.setUserImg(R.mipmap.user_img2);
            guestBook4.setUserId("BDL 4726-134-1");
            guestBook4.setUserMessage("干饭的一天呦！！！");
            guestBook4.setMessageTime("2022-11-17 08:27:13");
            guestBook4.setLikeNum("13");
            myList.add(guestBook4);

            GuestBookEntity guestBook9 = new GuestBookEntity();
            guestBook9.setUserImg(R.mipmap.user_img1);
            guestBook9.setUserId("HD 489-1622-1");
            guestBook9.setUserMessage("今天是代码的世界！");
            guestBook9.setMessageTime("2022-11-09 16:20:06");
            guestBook9.setLikeNum("87");
            myList.add(guestBook9);

            Constant.guestBookList = myList;
            return myList;
        } else {
            myList = Constant.guestBookList;
            return myList;
        }
    }

    private void setView() {
        listview = getActivity().findViewById(android.R.id.list);
        tvRemain = getActivity().findViewById(R.id.tv_remind);
        share = getActivity().findViewById(R.id.ib_message_share);
//        GuestBookToRoomDetail = getActivity().findViewById(R.id.ib_message_return);
        MessageDelete = getActivity().findViewById(R.id.btn_message_delete);
        PostGuestBook = getActivity().findViewById(R.id.btn_post_guestbook);
        tvLikeNumber = getActivity().findViewById(R.id.tv_like_number);
    }
}
